package com.zwh.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 路由配置
 */
@Configuration
public class GeteWayConfig {

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
        return routeLocatorBuilder.routes()
                .route("payment-route", r -> r.path("/payment/**")
                        .filters(f -> f.rewritePath("/payment/(?<segment>.*)", "/${segment}")) //(?<segment>.*) 为命名捕获组，匹配/payment/后的任意字符序列,示例：/payment/123 → 捕获组segment值为123
                        .uri("lb://payment-service"))
                .route("fallback-route", r -> r.path("/fallback/**")
                        .filters(f -> f.circuitBreaker(
                                config -> config.setName("myCircuitBreaker") // 定义熔断器唯一标识
                                        .setFallbackUri("forward:/fallback/user")// 指定降级端点
                        )) //配置熔断器
                        .uri("http://fallback.example.com"))
                .build();
    }
}
